package com.pentofab.payrollmanagement.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.pentofab.payrollmanagement.dao.EmployeeDao;
import com.pentofab.payrollmanagement.dao.model.BankInfo;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.Department;
import com.pentofab.payrollmanagement.dao.model.Designation;
import com.pentofab.payrollmanagement.dao.model.EmpCategory;
import com.pentofab.payrollmanagement.dao.model.Employee;
import com.pentofab.payrollmanagement.dao.model.Units;
import com.pentofab.payrollmanagement.database.EmployeeRepository;
import com.pentofab.payrollmanagement.database.impl.EmployeeRepositoryImpl;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

// TODO: Auto-generated Javadoc
/**
 * The Class EmployeeMgmtImpl.
 */
public class EmployeeDaoImpl implements EmployeeDao {

	/** The employee repository. */
	EmployeeRepository employeeRepository = null;

	/**
	 * Instantiates a new employee mgmt impl.
	 */
	public EmployeeDaoImpl() {

		employeeRepository = new EmployeeRepositoryImpl();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.PayRoll.PayRollMgmt.EmployeeMgmt#getAllEmployeeInfo()
	 */
	@Override
	public ArrayList<Employee> getAllEmployees() throws BusinessException {

		try {
			return employeeRepository.getAllEmployeeInfo();
		} catch (Exception e) {
			throw new BusinessException(ERROR_WHILE_GETTING_EMP_INFO);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.PayRoll.PayRollMgmt.EmployeeMgmt#getEmpById(java.lang.String)
	 */
	@Override
	public Employee getEmpById(String empId) throws BusinessException {

		// Validates the employee for format XX:YYYY
		PayRollUtility.validateEmpId(empId);
		try {
			return employeeRepository.getEmpById(empId);
		} catch (BusinessException exception) {

			throw exception;
		} catch (Exception exception) {
			throw new BusinessException(exception, ERROR_WHILE_GETTING_EMP_INFO
					+ empId);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.PayRoll.PayRollMgmt.EmployeeMgmt#getEmpByName(java.lang.String)
	 */
	@Override
	public ArrayList<Employee> getEmpByName(String companyCode, String empName,
			String operation) throws BusinessException {

		PayRollUtility.validateEmpName(empName);

		try {
			return employeeRepository.getEmpByName(companyCode, empName,
					operation);
		} catch (BusinessException exception) {

			throw exception;
		} catch (Exception exception) {
			exception.printStackTrace();
			throw new BusinessException(exception, ERROR_WHILE_GETTING_EMP_INFO
					+ empName);
		}
	}

	@Override
	public ArrayList<Employee> getAllCompanyEmployees(String companyCode)
			throws Exception {

		try {
			return employeeRepository.getAllCompanyEmployees(companyCode);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void addEmploye(Employee employee,boolean updateNextEmpNo) throws Exception {
		try {

			employeeRepository.addEmploye(employee, updateNextEmpNo);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateEmployee(Employee employee) throws Exception {
		// PayRollUtility.validateEmployee(employee);

		try {
			System.out.println(" : updateEmployee : "+employee);
			employeeRepository.updateEmployee(employee);
		} catch (BusinessException exception) {

			throw exception;
		} catch (Exception exception) {
			throw new BusinessException(exception, ERROR_WHILE_ADDING_EMP_INFO);
		}

	}

	@Override
	public void deleteEmployee(String employeeId, String userName) throws Exception {
		PayRollUtility.validateEmpId(employeeId);
		try {
			employeeRepository.deleteEmployee(employeeId, userName);
		} catch (BusinessException exception) {

			throw exception;
		} catch (Exception exception) {
			throw new BusinessException(exception,
					ERROR_WHILE_DELETING_EMPLOYEE + employeeId);
		}
	}

	@Override
	public void addDepartment(Department department) throws Exception {
		try {
			employeeRepository.addDepartment(department);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Department getDepartment(String companyCode, String code)
			throws Exception {
		try {
			return employeeRepository.getDepartment(companyCode, code);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void deleteDepartment(String companyCode, String code)
			throws Exception {
		try {
			employeeRepository.deleteDepartment(companyCode, code);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateDepartment(Department department) throws Exception {
		try {
			employeeRepository.updateDepartment(department);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<Department> getAllDepartments(String companyCode)
			throws Exception {

		try {
			return employeeRepository.getAllDepartments(companyCode);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public void addDesignation(Designation design) throws Exception {
		try {
			employeeRepository.addDesignation(design);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public Designation getDesignation(String companyCode, String code)
			throws Exception {

		try {
			return employeeRepository.getDesignation(companyCode, code);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public List<Designation> getAllDesignation(String companyCode)
			throws Exception {

		try {
			return employeeRepository.getAllDesignation(companyCode);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void deleteDesignation(String companyCode, String code)
			throws Exception {
		try {
			employeeRepository.deleteDesignation(companyCode, code);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
	public void updateDesignation(Designation design) throws Exception {
		try {
			employeeRepository.updateDesignation(design);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void addEmpCategory(EmpCategory cateogry) throws Exception {
		try {
			employeeRepository.addEmpCategory(cateogry);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public EmpCategory getEmpCategory(String companyCode, String code)
			throws Exception {

		try {
			return employeeRepository.getEmpCategory(companyCode, code);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<EmpCategory> getAllEmpCategory(String companyCode)
			throws Exception {

		try {
			return employeeRepository.getAllEmpCategory(companyCode);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void deleteEmpCategory(String companyCode, String code)
			throws Exception {
		try {
			employeeRepository.deleteEmpCategory(companyCode, code);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateEmpCategory(EmpCategory empCategory) throws Exception {
		try {
			employeeRepository.updateEmpCategory(empCategory);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void addCompany(Company company) throws Exception {
		try {
			employeeRepository.addCompany(company);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<Company> getCompanies() throws Exception {
		try {
			return employeeRepository.getCompanies();
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateCompany(Company company) throws Exception {
		try {
			employeeRepository.updateCompany(company);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Company getCompany(String companyCode) throws Exception {
		try {
			return employeeRepository.getCompany(companyCode);
		} catch (Exception e) {
			throw e;
		}
	}
	
	@Override
	public Units getUnit(String unitCode, String compCode) throws Exception {
		try {
			return employeeRepository.getUnit(unitCode, compCode);
		} catch (Exception e) {
			throw e;
		}
	}
	@Override
	public String getNextEmpID(String companyCode) throws BusinessException {
		try {
			return employeeRepository.getNextEmpId(companyCode);
		} catch (Exception e) {
			throw new BusinessException(e, ERROR_WHILE_GETTING_NEXT_EMP_ID);
		}
	}

	@Override
	public void addUnit(Units units) throws Exception {
		try {
			employeeRepository.addUnit(units);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public ArrayList<Units> getUnits(String companyCode) throws Exception {
		try {
			return employeeRepository.getUnits(companyCode);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateUnit(Units units) throws Exception {
		try {
			employeeRepository.updateUnit(units);
		} catch (Exception e) {
			throw e;
		}
	}

	
	public void addBank (BankInfo bankInfo) throws Exception{
		try {
			employeeRepository.addBank(bankInfo);
		}catch (Exception e) {
			throw e;
		}
	}
	
	public ArrayList<BankInfo> getBankInfo(String bankCode) throws Exception {
		try {
			return employeeRepository.getBankInfo(bankCode);
		} catch (Exception e) {
			throw e;
		}
	}
	public void updateBankInfo(BankInfo bankInfo) throws Exception {
		try {
			employeeRepository.updateBankInfo(bankInfo);
		} catch (Exception e) {
			throw e;
		}
	}
	
	

}
